/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package au.edu.swin.unilearn;

import au.edu.swin.unilearn.subject.*;
import au.edu.swin.unilearn.tutorialGroup.*;
import au.edu.swin.unilearn.assignment.*;
import au.edu.swin.unilearn.submission.*;
import au.edu.swin.unilearn.students.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.model.SelectItem;
 


/**
 *
 * @author Ting Wang
 */

public class ExtensionManagedBean {
    
    private static String STAFFID = "912345X";
    @EJB
    private TutorialGroupSessionRemote tutorialGroupSession;
    @EJB
    private AssignmentSessionRemote assignmentSession;
    @EJB
    private SubjectSessionRemote subjectSession;
    private String filterSubjectCode;
    private String subjectName;
    private String filterAssignment;
    private String assignmentName;
    private Assignment assignmentObj;
    private String deadLineToString;
    private Date extension;

    public List<SelectItem> getSubjectCodeItems()
    {
        List<SelectItem> subjectCodeItems = new ArrayList();
        for (TutorialGroup tg : tutorialGroupSession.getTutorialGroupOf(STAFFID))
        {
            Subject s = tg.getSubject();
            SelectItem si = new SelectItem(s.getSubject_code(), s.getSubject_code() + " - " + s.getSubject_name());
            subjectCodeItems.add(si);
        }
        return subjectCodeItems;
    }

    public List<SelectItem> getAssignmentItems()
    {
        List<SelectItem> assignmentItems = new ArrayList();
        System.out.println("Subject Code: " + getFilterSubjectCode());
        for (Assignment a : assignmentSession.getAssignments(getFilterSubjectCode()))
        {
            SelectItem si = new SelectItem(a.getAssignment_no(), a.getAssignment_no() + " - " + a.getAssignment_name());
            assignmentItems.add(si);
        }
        return assignmentItems;
    }


    public String getAssignmentAvailable()
    {
        Subject s = subjectSession.getSubject(getFilterSubjectCode());
        this.setSubjectName(s.getSubject_name());
        if((getAssignmentItems()).size() == 0)
        {
            return "noAssignment";
        }
        return "next";
    }
    
    public String retieveAssignment()
    {
        assignmentObj = assignmentSession.getAssignment(filterAssignment, filterSubjectCode);
        System.out.println("DATE = " + assignmentObj.getDeadline());
        System.out.println("TIME = " + assignmentObj.getDeadline().getTime());
        return "nextPage";
    }
    
    public String updateExtension()
    {
        try
        {
            assignmentObj.setExtendedDeadLine(extension);
            assignmentSession.updateAssignment(assignmentObj);
            return "success";
        }
        catch(Exception e)
        {
            return "failure";
        }
    }

    

    /** Creates a new instance of ExtensionManagedBean */
    public ExtensionManagedBean() {
    }

    public String getFilterSubjectCode() {
        return filterSubjectCode;
    }

    public void setFilterSubjectCode(String filterSubjectCode) {
        this.filterSubjectCode = filterSubjectCode;
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

    public String getFilterAssignment() {
        return filterAssignment;
    }

    public void setFilterAssignment(String filterAssignment) {
        this.filterAssignment = filterAssignment;
    }

    public String getAssignmentName() {
        return assignmentName;
    }

    public void setAssignmentName(String assignmentName) {
        this.assignmentName = assignmentName;
    }

    public Assignment getAssignmentObj() {
        return assignmentObj;
    }

    public void setAssignmentObj(Assignment assignmentObj) {
        this.assignmentObj = assignmentObj;
    }

    public Date getExtension() {
        return extension;
    }

    public void setExtension(Date extension) {
        this.extension = extension;
    }

    public String getDeadLineToString() {
        deadLineToString = (assignmentObj.getDeadline()).toString();
        return deadLineToString;
    }

    public void setDeadLineToString(String deadLineToString) {
        this.deadLineToString = deadLineToString;
    }

}
